<template>
  <div>
    <a-input v-model="name" :placeholder="placeholder || $t('m.common.label.option_select')"
             @click="selectViewCode"
             readOnly>
      <ych-icon slot="addonAfter" type="setting"/>
    </a-input>

    <a-badge v-if="useParameter && parameterConfig.length > 0" :dot="true">
      <a-button type="link" size="small" @click="outParameterInput">{{ $t('m.eform.formDesign.label.chu-can-pei-zhi') }}</a-button>
    </a-badge>

    <select-view-part-dialog ref="comp" @selectFinished="selectViewOk" :groupId="groupId"></select-view-part-dialog>

    <out-parameter-dialog ref="outParameterDialog" @selectFinished="outParameterOk"></out-parameter-dialog>
  </div>
</template>

<script>

  import { getAction } from '@/api/manage'
  import YchConstant from '@/api/YchConstant'
  import SelectViewPartDialog from '../dialog/SelectViewPartDialog'
  import OutParameterDialog from "../../modules/actAppView/OutParameterDialog";

  export default {
    name: 'ViewCodeTag',
    components: {
      SelectViewPartDialog,
      OutParameterDialog
    },
    props: {
      value: {
        type: String,
        default: null
      },
      viewCode: {
        type: String,
        default: null
      },
      placeholder: {
        type: String,
      },
      groupId: {
        type: String,
        required: true
      },
      useParameter: {
        type: Boolean,
        default: false
      },
      parameterConfig: {
        type: Array,
        default: () => []
      },
      /*暂时没用，后面做自动选择时使用
      parameters: {
        type: Array,
        default: () => []
      },*/
    },
    data() {
      return {
        name: null,
        url:{
          getInParameterJson: YchConstant.PREFIX_EFORM + '/manage/actAppView/getInParameterJson'
        }
      }
    },
    watch: {
      value: {
        handler(value) {
          this.name = value
        },
        immediate: true
      }
    },
    methods: {
      selectViewCode() {
        this.$refs.comp.radio = true
        this.$refs.comp.visible = true
      },
      selectViewOk(selectedRowKeys, selectionRows) {
        this.$emit('selectFinished', selectedRowKeys, selectionRows)

        // 获取入参配置
        getAction(this.url.getInParameterJson, {viewCode: selectionRows[0].viewCode}).then(res => {
          if (res.success) {
            if (!res.result) {
              return
            }
            const newDataSource = JSON.parse(res.result)

            newDataSource.forEach(item => {
              const outData = this.parameterConfig.find(vo => vo.parameterKey === item.parameterKey)
              if (outData) {
                item.outKey = outData.outKey;
                item.defaultValue = outData.defaultValue;
                item.isRequired = outData.isRequired;
              } else {
                item.outKey = "";
              }
            })
            this.$emit("setParameterConfig", newDataSource);
          } else {
            this.messageUtil.warning(res.message)
          }
        })
      },
      outParameterInput () {
        if(!this.viewCode){
          this.messageUtil.warning(this.$t('m.eform.appview.verify.link_page'))
          return
        }
        this.$refs.outParameterDialog.open(JSON.stringify(this.parameterConfig), this.viewCode)
      },
      outParameterOk (json) {
        const parameterConfig = JSON.parse(json)
        this.$emit("setParameterConfig", parameterConfig);
      },
    }
  }
</script>
